﻿using System;
using System.IO;
using System.Reflection;
using System.Threading;
using log4net;
using System.Configuration;

namespace NetClientFramework.Helper
{
    public static class LogHelper
    {
        private static readonly ILog Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

        private static bool IsDebug()
        {
            try
            {
                string isDebug = ConfigurationManager.AppSettings["IsDebug"];
                if (!string.IsNullOrEmpty(isDebug))
                {
                    bool debug = Convert.ToBoolean(isDebug);
                    return debug;
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                return false;
            }

            return false;
        }

        public static void Init()
        {
            try
            {
                log4net.Config.XmlConfigurator.Configure();
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }

        }

        public static void Info(object message)
        {
            if (IsDebug())
            {
                try
                {
                    Log.Info(message);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                    throw;
                }

            }

        }

        public static void Info(object message, Exception exception)
        {
            if (IsDebug())
            {
                try
                {
                    Log.Info(message, exception);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                    throw;
                }

            }
        }

        public static void Error(object message)
        {
            try
            {
                Log.Error(message);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }

        public static void Error(object message, Exception exception)
        {
            try
            {
                Log.Error(message, exception);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }

    }
}